package cm309.BookingSystem.tm.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cm309.BookingSystem.common.Common;
import cm309.BookingSystem.common.MyConnection;
import cm309.BookingSystem.tm.model.*;
import cm309.BookingSystem.web.Action;




public class ReadTMAction implements Action {

	List<Trainer> trainers=new ArrayList<Trainer>();	
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)   {
		MyConnection con=new MyConnection();
		try {
				
			ResultSet rs=con.getPS("select TRAINERID ,TRAINERNAME ,TRAINERGENDER ,TRAINERAGE " +
				           " ,CLASSIFICATION From BK_TM_TRAINER")
			               .executeQuery();
			
			Trainer t=new Trainer();
			trainers.clear();
			while (rs.next()) {
			t=new Trainer();
			t.setTrainerID(rs.getString("TRAINERID"));
			t.setTrainerName(rs.getString("TRAINERNAME"));
			char[] cbuf=new char[1];
			rs.getCharacterStream("TRAINERGENDER").read(cbuf);
			t.setTrainerGender(cbuf[0]);
			t.setTrainerAge(rs.getInt("TRAINERAGE"));
			t.setClassification(rs.getString("CLASSIFICATION"));
			
			trainers.add(t);
			
			}
			con.commit();
            con.PSclose();
			PrintWriter w = response.getWriter();
			
			w.println(Common.beanToJson(trainers));
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
		  con.close();
		}

	}

}
